package com.have.to.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.have.to.vm.BaseVM;
import com.hjq.demo.app.TitleBarFragment;

import java.util.List;


public abstract class AppVmFragment<VM extends BaseVM> extends TitleBarFragment implements BaseUI<VM> {
    protected VM mCurrentVM;
    protected Context mContext;
    public boolean isVMFragment;

    @Override
    public void onAttach(Context context) {
        mContext = context;
        super.onAttach(context);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        new BaseUIHelper<VM>().init(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    //region ViewModel code
    @Override
    public <M extends ViewModel> M getVM(Class<M> cls) {
        if (isVMFragment) {
            return new ViewModelProvider(this,
                    ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(cls);
        } else {
            return new ViewModelProvider(this,
                    ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(cls);
        }
    }

    public void setVMFragment(boolean isVMFragment) {
        this.isVMFragment = isVMFragment;
    }

    /**
     * 可选的，初始化多个ViewModel的统一入口
     *
     * @return
     */
    @Override
    public List<BaseVM> initVMList() {
        return null;
    }

    @Override
    public void setCurrentVM(VM currentVM) {
        this.mCurrentVM = currentVM;
    }

    @Override
    public void finish() {
        if (getActivity() != null) {
            getActivity().finish();
        }
    }

    //endregion

    /**
     * 替换区域fragment
     *
     * @param resId
     * @param fragment
     */
    public void replaceFragment(int resId, Fragment fragment) {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(resId, fragment);
        transaction.commit();
    }

    @Override
    public LifecycleOwner getLifecycleOwner() {
        return this;
    }

    @Override
    public boolean isStatusBarEnabled() {
        return !super.isStatusBarEnabled();
    }

    @Nullable
    @Override
    public Context getContext() {
        Context context = super.getContext();
        if (context == null) {
            context = mContext;
        }
        return context;
    }
}
